

package eu.itcsolutions.android.tutorial;


import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;



import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
// import org.json.JSONObject;
import org.json.JSONStringer;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

public class TransferaServer extends Activity {
	
	 Data dm;
	 List<String[]> ordersToSend =null ;
	 //String[] StringOfOrders;

        private final static String SERVICE_URI = "http://ftp.pangram.ro:9090/SalesService/SalesService.svc";

        @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                setContentView(R.layout.main);
                dm = new Data(this);
                ordersToSend = dm.selectAllOrders();
                String isNot =new String("");
               
            HttpPost request = new HttpPost(SERVICE_URI + "/json/addorder");
            request.setHeader("Accept", "application/json");
            request.setHeader("Content-type", "application/json");
            
        
            for (String[] orderToSend : ordersToSend){
            	String idLineToSend = orderToSend[0];
            	String clientToSend = orderToSend[1];
            	String productToSend = orderToSend[2];
            	String piecesToSend = orderToSend[3];
            	String discountToSend = orderToSend[4];
            
            
            
            
            
            //   String not = new String(" ");
           
           // String[] ComandaDeTrimis = ordersToSend.get(0);
          //  String ClientDeTrimis = ComandaDeTrimis[0];
          //  String ProdusDeTrimis = ComandaDeTrimis[1];
          //  String NumarBucati = ComandaDeTrimis[2];
          // String Discount = ComandaDeTrimis[3];
            
           
            
          
            try {
            // Build JSON string
           // JSONStringer vehicle = new JSONStringer()
            	JSONStringer jsonOrderToSend = new JSONStringer()
                .object()
                    .key("od")
                        .object()
                            .key("ClientName").value(clientToSend)
                            .key("ProductName").value(productToSend)
                             .key("PiecesNumber").value(piecesToSend)
                              .key("DiscountNumber").value(discountToSend)
                               .key("LineOrderId").value(idLineToSend)
                        .endObject()
                    .endObject();
            
            StringEntity entity = new StringEntity(jsonOrderToSend.toString());
            
           // Toast.makeText(this, vehicle.toString() + "\n", Toast.LENGTH_LONG).show() ;
            
            Log.d(" OrderLine " , jsonOrderToSend.toString() + "\n" );
            
            request.setEntity(entity);
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = httpClient.execute(request);
            // Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode());
           // Toast.makeText(this, response.getStatusLine().getStatusCode() + "\n", Toast.LENGTH_LONG).show() ;
            Log.d("Webinvoke", " OK if 200 = "+ response.getStatusLine().getStatusCode());
            }catch (Exception e) {
                isNot = "NOT ";
            } 
            
            Toast.makeText(this, isNot + " Comenzi transferate cu succes. " + "\n", Toast.LENGTH_LONG).show() ;
            
            
        
        }    
}
}